package com.example.controller;


import com.example.dao.ClientsMapper;
import com.example.dao.UserMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.entity.Clients;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import org.apache.shiro.authz.annotation.RequiresPermissions;

@CrossOrigin
@RestController
@RequestMapping("/Client")

public class ClientsController {

    @Autowired
    private ClientsMapper clientsMapper;
    @Autowired
    private UserMapper userMapper;
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @RequestMapping("getClients/{id}")
    public String getClient(@PathVariable int id){
        return clientsMapper.selectByPrimaryKey(id).toString();
    }

    @GetMapping(value = "getClientsAll/{page}",produces = "text/html;charset=UTF-8")
    public String getClientAll(@PathVariable int page) throws Exception{
        Map<String, Object> data = new HashedMap();
        data.put("currIndex",(page-1)*10);
        data.put("pageSize",10);
        List<Clients> cList = clientsMapper.selectAll(data);
        for (Clients a:cList) {
            if (a.getLastDealTime() != null) {
                a.setLastDealTimeText(sdf.format(a.getLastDealTime()));
            }
            if (a.getAssumeRecycleTime() != null) {
                a.setAssumeRecycleTimeText(sdf.format(a.getAssumeRecycleTime()));
            }
            if (a.getGetDispatchTime() != null) {
                a.setGetDispatchTimeText(sdf.format(a.getGetDispatchTime()));
            }
            if (a.getOwnerChangeTime() != null) {
                a.setOwnerChangeTimeText(sdf.format(a.getOwnerChangeTime()));
            }
            if (a.getRecycleTime() != null) {
                a.setRecycleTimeText(sdf.format(a.getRecycleTime()));
            }
            if (a.getLastProcessTime() != null) {
                a.setLastProcessTimeText(sdf.format(a.getLastProcessTime()));
            }
            if (a.getCreateTime() != null) {
                a.setCreateTimeText(sdf.format(a.getCreateTime()));
            }
            if (a.getLastModifyTime() != null) {
                a.setLastModifyTimeText(sdf.format(a.getLastModifyTime()));
            }

        }
        cList.stream().forEach(a->{
            if (a.getClient_eid() != null && a.getClient_eid() != ""){
                a.setClient_eid(userMapper.selectByName(a.getClient_eid()).getRealname());
            }
        });
        ObjectMapper mapper = new ObjectMapper();
        String JSONString =  mapper.writeValueAsString(cList);
        return JSONString;
    }

    @GetMapping(value = "getClientsCount",produces = "text/html;charset=UTF-8")
    public String getClientCounts() throws Exception{
        int count = clientsMapper.count();
        ObjectMapper mapper = new ObjectMapper();
        String JSONString =  mapper.writeValueAsString(count);
        return JSONString;
    }


    @RequiresPermissions("super")
    @GetMapping(value = "deleteClient/{id}",produces = "text/html;charset=UTF-8")
    public String deleteClient(@PathVariable int id) throws Exception{
        int result = clientsMapper.deleteByPrimaryKey(id);
        ObjectMapper mapper = new ObjectMapper();
        String JSONString =  mapper.writeValueAsString(result);
        return JSONString;
    }

    @GetMapping(value = "moveToSea/{id}",produces = "text/html;charset=UTF-8")
    public String moveToSea(@PathVariable int id) throws Exception{
        int result = clientsMapper.moveToSea(id);
        ObjectMapper mapper = new ObjectMapper();
        String JSONString =  mapper.writeValueAsString(result);
        return JSONString;
    }


    @GetMapping(value = "search/{info}",produces = "text/html;charset=UTF-8")
    public String search(@PathVariable String info) throws Exception{
        List<Clients> cList  = clientsMapper.search(info);
        ObjectMapper mapper = new ObjectMapper();
        String JSONString =  mapper.writeValueAsString(cList);
        return JSONString;
    }

    @PutMapping(produces = "text/html;charset=UTF-8")
    public String updateClient(@RequestBody Clients client) throws IOException {
        int result = clientsMapper.updateByPrimaryKeySelective(client);
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(result);
    }

    @RequestMapping(value="add",produces = "text/html;charset=UTF-8")
    public String addClient(@RequestBody Clients client) throws IOException {
        String repeatHint = "";
        boolean isRepeatCompany = false;
        boolean isRepeatMobile = false;
        Clients a = clientsMapper.selectByCompany(client.getClient());
        Clients b = clientsMapper.selectByMobile(client.getPhone());
        if(a != null && a.getId() != null){
            isRepeatCompany = true;
        }
        if(b != null && b.getId() != null){
            isRepeatMobile = true;
        }
        if(isRepeatCompany || isRepeatMobile){
            if(isRepeatCompany){
                repeatHint += "已存在重复客户名称";
            }
            if(isRepeatMobile){
                repeatHint += "已存在重复联系手机";
            }
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(repeatHint);
        }
        else{
            int result = clientsMapper.insert(client);
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(result);
        }

    }

    @GetMapping(value = "getSea/{page}",produces = "text/html;charset=UTF-8")
    public String selectSea(@PathVariable int page) throws Exception{
        Map<String, Object> data = new HashedMap();
        data.put("currIndex",(page-1)*10);
        data.put("pageSize",10);
        List<Clients> cList = clientsMapper.selectSea(data);
        for (Clients a:cList) {
            if (a.getLastDealTime() != null) {
                a.setLastDealTimeText(sdf.format(a.getLastDealTime()));
            }
            if (a.getAssumeRecycleTime() != null) {
                a.setAssumeRecycleTimeText(sdf.format(a.getAssumeRecycleTime()));
            }
            if (a.getGetDispatchTime() != null) {
                a.setGetDispatchTimeText(sdf.format(a.getGetDispatchTime()));
            }
            if (a.getOwnerChangeTime() != null) {
                a.setOwnerChangeTimeText(sdf.format(a.getOwnerChangeTime()));
            }
            if (a.getRecycleTime() != null) {
                a.setRecycleTimeText(sdf.format(a.getRecycleTime()));
            }
            if (a.getLastProcessTime() != null) {
                a.setLastProcessTimeText(sdf.format(a.getLastProcessTime()));
            }
            if (a.getCreateTime() != null) {
                a.setCreateTimeText(sdf.format(a.getCreateTime()));
            }
            if (a.getLastModifyTime() != null) {
                a.setLastModifyTimeText(sdf.format(a.getLastModifyTime()));
            }

        }
        cList.stream().forEach(a->{
            if (a.getClient_eid() != null && a.getClient_eid() != ""){
                a.setClient_eid(userMapper.selectByName(a.getClient_eid()).getRealname());
            }
        });
        ObjectMapper mapper = new ObjectMapper();
        String JSONString =  mapper.writeValueAsString(cList);
        return JSONString;
    }

    @GetMapping(value = "getSeaCount",produces = "text/html;charset=UTF-8")
    public String getSeaCounts() throws Exception{
        int count = clientsMapper.seacount();
        ObjectMapper mapper = new ObjectMapper();
        String JSONString =  mapper.writeValueAsString(count);
        return JSONString;
    }

    @RequiresPermissions("super")
    @GetMapping(value = "getClientsDropAll/{page}",produces = "text/html;charset=UTF-8")
    public String getClientDropAll(@PathVariable int page) throws Exception{
        Map<String, Object> data = new HashedMap();
        data.put("currIndex",(page-1)*10);
        data.put("pageSize",10);
        List<Clients> cList = clientsMapper.selectClientDrop(data);
        ObjectMapper mapper = new ObjectMapper();
        String JSONString =  mapper.writeValueAsString(cList);
        return JSONString;
    }

    @RequiresPermissions("super")
    @GetMapping(value = "getClientsDropCount",produces = "text/html;charset=UTF-8")
    public String getClientDropCounts() throws Exception{
        int count = clientsMapper.dropcount();
        ObjectMapper mapper = new ObjectMapper();
        String JSONString =  mapper.writeValueAsString(count);
        return JSONString;
    }

    @GetMapping(value = "recover/{id}",produces = "text/html;charset=UTF-8")
    public String recoveryClient(@PathVariable int id) throws Exception{
        Clients c = new Clients();
        c.setId(id);
        c.setIsDrop("F");
        int count = clientsMapper.updateByPrimaryKeySelective(c);
        ObjectMapper mapper = new ObjectMapper();
        String JSONString =  mapper.writeValueAsString(count);
        return JSONString;
    }

    @RequiresPermissions("super")
    @GetMapping(value = "drop/{id}",produces = "text/html;charset=UTF-8")
    public String dropClient(@PathVariable int id) throws Exception{
        Clients c = new Clients();
        c.setId(id);
        c.setIsDrop("T");
        int count = clientsMapper.updateByPrimaryKeySelective(c);
        ObjectMapper mapper = new ObjectMapper();
        String JSONString =  mapper.writeValueAsString(count);
        return JSONString;
    }

}
